package cloud.seri.gateway.security.oauth2

import javax.servlet.http.Cookie
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletRequestWrapper

/**
 * A request mapper used to modify the cookies in the original request.
 * This is needed such that we can modify the cookies of the request during a token refresh.
 * The token refresh happens before authentication by the `OAuth2AuthenticationProcessingFilter`
 * so we must make sure that further in the filter chain, we have the new cookies and not the expired/missing ones.
 */
internal class CookiesHttpServletRequestWrapper(request: HttpServletRequest,
                                                /**
                                                 * The new cookies of the request. Use these instead of the ones found in the wrapped request.
                                                 */
                                                private val cookies: Array<Cookie>) : HttpServletRequestWrapper(request) {

    /**
     * Return the modified cookies instead of the original ones.
     * @return the modified cookies.
     */
    override fun getCookies(): Array<Cookie> {
        return cookies
    }
}
